package com.lw.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.lw.ssm.dto.R;
import com.lw.ssm.pojo.Score;
import com.lw.ssm.service.ScoreService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Administrator
 * @version 1.0
 * @description:
 * @date 2022/3/12 11:30
 */
@RestController
@RequestMapping("/score")
public class ScoreController {

    private Logger logger = LoggerFactory.getLogger(ScoreController.class);

    private ScoreService scoreService;

    @Autowired
    public void setScoreService(ScoreService scoreService) {
        this.scoreService = scoreService;
    }

    @GetMapping("/find/{id}")
    public R findById(@PathVariable Long id){
        logger.error("查询方法执行了，当前id为{}",id);
        List<Score> score = scoreService.findById(id);
        return R.setOK(score);
    }

    @GetMapping("/find")
    public R findAll(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "2") int pageSize){
        PageInfo<Score> pageInfo = scoreService.findAll(pageNum, pageSize);
        logger.error("分页查询执行了，当前页为{}，显示{}条数据",pageNum,pageSize);
        return R.setOK(pageInfo);
    }

    @PostMapping("/add")
    public R addScore(@RequestBody Score score){
        scoreService.addScore(score);
        return R.setOK();
    }

    @PostMapping("/add/update")
    public R updateScore(@RequestBody Score score){
        scoreService.updateScore(score);
        return R.setOK();
    }

    @GetMapping("/find/del/{id}")
    public R deleteScore(@PathVariable Long id){
        scoreService.deleteScore(id);
        return R.setOK();
    }
}
